﻿using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using HHControls.CreditEducation;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Commons;
using HHPS.CreditEducation.Presentation.StudyPlans;

namespace HHPS.CreditEducation.Presentation
{
    public partial class MainView : HHMainForm
    {
        public static MainView Instance;
        public Business.Business CreditEducationBusiness { get; set; }

        private const string FIRST_OPEN = "QL kế hoạch";

        #region Constructor

        public MainView()
        {
            InitializeComponent();

            CreditEducationBusiness = new Business.Business();

            Instance = this;

            RefreshWhenAddStudyPlan();

            ShowUser();
        }

        private void ShowUser()
        {
            var user = CreditEducationBusiness.DataContex.Users.FirstOrDefault();
            if (user == null)
            {
                lblUserName.Text = "Chưa có tên người dùng";
            }
            else
            {
                lblUserName.Text = user.Name;
                if (user.Picture != null)
                {
                    ptbAvatar.Image = Utils.ByteArrayToImage(user.Picture);
                }
            }
        }

        #endregion

        #region Public Methods

        public void AddUserControlToPanel(Control userControl)
        {
            pnlMain.Controls.Clear();
            pnlMain.Controls.Add(userControl);
        }

        public void ShowTextCurrentModule(string moduleText)
        {
            lblCurrentModule.Text = moduleText;
        }

        public void ApplyStatus(string status)
        {
            lblStatus.Text = status;
        }

        public void ApplyChangeUserInformation(string name, Bitmap image)
        {
            lblUserName.Text = name;
            ptbAvatar.Image = image;
        }

        public void RefreshWhenAddStudyPlan()
        {
            Navigation.Initialize();
            foreach (StudyPlan studyPlan in CreditEducationBusiness.DataContex.StudyPlans)
            {
                Navigation.NavItems.Add(new NavItem
                                            {
                                                Name = studyPlan.Name,
                                                GroupName = "expStudyPlans",
                                                ShowingControl = new ElementControl(studyPlan.Name)
                                            });
            }
            Navigation.Show(pnlLeft.Controls);
            Navigation.ExpandGroup(Navigation.NavGroups.First(g => g.Name == "expStudyPlans"));

            if (pnlMain.Controls.Count < 1)
            {
                Navigation.ShowControlOnPanel(Navigation.NavItems.First(n => n.Name == FIRST_OPEN));
            }
            else
            {
                Navigation.NavItems.First(n => n.Name == FIRST_OPEN).Instance.IsClicked = true;
            }
        }

        public void GoToStudyPlanScreen(string name)
        {
            Navigation.ExpandGroup(Navigation.NavGroups.First(g => g.Name == "expStudyPlans"));

            Navigation.ShowControlOnPanel(Navigation.NavItems.First(n => n.Name == name));
        }

        #endregion
    }
}